package cn.littlehx.simple_score_manage.api;

import cn.littlehx.simple_score_manage.common.R;
import cn.littlehx.simple_score_manage.common.REnum;
import cn.littlehx.simple_score_manage.common.RunException;
import cn.littlehx.simple_score_manage.domain.Course;
import cn.littlehx.simple_score_manage.domain.Teacher;
import cn.littlehx.simple_score_manage.service.ITeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/teacher")
public class TeacherController {
    @Autowired
    private ITeacherService teacherService;

    /**
     * 获取老师所教学的所有课程信息
     * @param request
     * @return
     */
    @GetMapping("/course")
    public R selectCourse(HttpServletRequest request){
        String teacherNo = (String) request.getSession().getAttribute("teacherNo");
        List<Course> courses = teacherService.selectCourseByTeacherNo(teacherNo);
        return R.success(courses);
    }

    @GetMapping("/course/id")
    public R selectCourseById(@RequestParam Integer id){
        Teacher byId = teacherService.getById(id);
        List<Course> courses = teacherService.selectCourseByTeacherNo(byId.getNo());
        return R.success(courses);
    }

    /**
     * 获取对应课程编号的学生等的信息
     * @param request
     * @param courseNo
     * @return
     */
    @GetMapping("/score")
    public R selectScore(HttpServletRequest request,String courseNo){
        List<Map<String, String>> maps = teacherService.selectScoreByCourseNo(courseNo);
        System.out.println(maps);
        return R.success(maps);
    }
    /**
     * 按照邮箱和密码进行登录
     * 纪录登录的id和no
     *
     * @param teacher
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Teacher teacher, HttpServletRequest request) throws RunException {
        Teacher teacherReturned = teacherService.selectByNoAndPassword(teacher.getNo(), teacher.getPassword());
        if (teacherReturned != null) {
            teacherReturned.setPassword("");
            //存入session
            request.getSession().setAttribute("id", teacherReturned.getId());
            request.getSession().setAttribute("teacherNo", teacherReturned.getNo());
            log.info("教师用户 id:" + teacherReturned.getId() + "  no:" + teacherReturned.getNo() + "  登录");
            return R.success(teacherReturned);
        } else throw new RunException(REnum.LOGIN_ERROR);
    }
    /**
     * 管理员登录
     * 按照邮箱和密码进行登录
     * 纪录登录的id和no
     *
     * @param teacher
     * @return
     */
    @PostMapping("/login/admin")
    public R loginAdmin(@RequestBody Teacher teacher, HttpServletRequest request) throws RunException {
        try {
            Teacher teacherReturned = teacherService.selectByNoAndPasswordAdmin(teacher.getNo(), teacher.getPassword());
            if (teacherReturned != null&&teacherReturned.getPriority()==1) {
                teacherReturned.setPassword("");
                //存入session
                request.getSession().setAttribute("id", teacherReturned.getId());
                request.getSession().setAttribute("teacherNo", teacherReturned.getNo());
                log.info("教师用户 id:" + teacherReturned.getId() + "  no:" + teacherReturned.getNo() + "  登录");
                return R.success(teacherReturned);
            } else throw new RunException(REnum.LOGIN_ERROR);
        }catch (Exception e){
            throw new RunException(REnum.LOGIN_ERROR);
        }

    }

    /**
     * 进行登出,清楚session中的Id和No
     *
     * @param
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpServletRequest request) {
        log.info("教师用户 工号:" + request.getSession().getAttribute("teacherNo") + "  已经退出");
        //清理session中的员工id和no
        request.getSession().removeAttribute("id");
        request.getSession().removeAttribute("teacherNo");
        return R.success();
    }

    /**
     * 分页读取全部内容
     *
     * @return
     */
    @GetMapping()
    public R<Page<Teacher>> getByPage(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size
            ,@RequestParam(defaultValue = "") String name ,@RequestParam(defaultValue = "") Integer sex,@RequestParam(defaultValue = "") String no) {
        page++;
        Page<Teacher> teacherPage = new Page<>(page, size);
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        if (name != null&& !name.equals("")) {
            wrapper.like(Teacher::getName, name);
        }
        if (sex != null&& sex!=0) {
            wrapper.like(Teacher::getSex, sex);
        }
        if (no != null&& !no.equals("")) {
            wrapper.like(Teacher::getNo, no);
        }
        //可在此添加筛选条件
        teacherService.page(teacherPage, wrapper);
        List<Teacher> records = teacherPage.getRecords();

        for (int i = 0; i < records.size(); i++) {
            records.get(i).setPassword("");
        }
        log.info("返回的数据条数为" + teacherPage.getSize());
        return R.success(teacherPage);
    }

    @PostMapping()
    public R add(@RequestBody Teacher teacher) throws RunException {
        //检查是否有重复的学号
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teacher::getNo, teacher.getNo());
        List<Teacher> list = teacherService.list(wrapper);
        if (teacher.getNo()==null|| teacher.getNo().equals("") ||!list.isEmpty()) {
            throw new RunException(REnum.REPETITIVE_NO);
        }
        teacherService.save(teacher);
        return R.success();

    }
    @GetMapping("/getOne")
    public R getAStudent(@RequestParam Integer id){
        Teacher byId = teacherService.getById(id);
        byId.setPassword("");
        return R.success(byId);
    }

    /**
     * 删除老师
     *
     * @param teacher
     * @return
     */
    @DeleteMapping()
    public R delete(@RequestBody Teacher teacher) {
        teacherService.removeById(teacher.getId());
        log.info("删除了一个老师");
        return R.success();
    }

    /**
     * 进行修改操作
     *
     * @param teacher
     * @return
     */
    @PutMapping()
    public R update(@RequestBody Teacher teacher) {
        teacherService.updateById(teacher);
        return R.success();
    }


}
